package com.xpgk.mvc.statistic.monthTotalStatistic;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.xpgk.annotation.Service;
import com.xpgk.mvc.base.BaseService;
import com.xpgk.mvc.bentryRegister.Customer;
import com.xpgk.mvc.bentryRegister.CustomerAgreement;
import com.xpgk.mvc.bentryRegister.CustomerDetail;
import com.xpgk.mvc.binShopService.CustomerService;
import com.xpgk.mvc.binShopService.CustomerServiceNurse;
import com.xpgk.mvc.codeSet.Code;
import com.xpgk.mvc.measurement.Temporary;
import com.xpgk.mvc.organization.Organization;
import com.xpgk.mvc.productSales.ProductSales;
import com.xpgk.mvc.productSet.Product;
import com.xpgk.mvc.statistic.dailyTotalStatistic.DailyNursingTotal;
import com.xpgk.mvc.statistic.yearTotalStatistic.YearTotal;
import com.xpgk.tools.ToolDateTime;

@Service(name = MonthTotalStatisticServices.serviceName)
public class MonthTotalStatisticServices extends BaseService {
	
	@SuppressWarnings("unused")
	private static final Log log = Log.getLog(MonthTotalStatisticServices.class);
	public static final String serviceName = "monthTotalStatisticServices";
	
	/**保存旧数据到月综合查询
	 * 
	 */
	public  void  saveOld()
	{
		String  sql  = "SELECT *  FROM  sb_customer_temporary and dataStatus in ('1','2') ";
		List<Temporary>  temporaryList = Temporary.dao.find(sql);
		for(Temporary t:temporaryList)
		{
			//通过测算机构和日期查询日综合查询表数据
			//保存更新顾客统计信息
			MonthTotal dnt = MonthTotal.dao.getMonthTotal(t.getStr(Temporary.column_organIds), t.getDate(Temporary.column_gxsj)!=null ? t.getDate(Temporary.column_gxsj) : t.getDate(Temporary.column_createDate));
			if(dnt!=null)
			{
				dnt.set(MonthTotal.column_zxrs, dnt.getInt(MonthTotal.column_zxrs)+1);//咨询人数
				if(t.getStr(Temporary.column_gkfl).equals(Code.cjCus_ids))
				{
					dnt.set(MonthTotal.column_cjrs, dnt.getInt(MonthTotal.column_cjrs)+1);//成交人数
					double  cjl = ((double)dnt.getInt(MonthTotal.column_cjrs))/((double)dnt.getInt(MonthTotal.column_zxrs));
					dnt.set(MonthTotal.column_xgkCjl,new DecimalFormat("#.00").format(cjl*100));//成交率
					if(t.getStr(Temporary.column_jdfs).equals(Code.zjs_ids))
					{
						dnt.set(MonthTotal.column_zjscjrs, dnt.getInt(MonthTotal.column_zjscjrs)+1);//转介绍成交人数
					}
					else if(t.getStr(Temporary.column_jdfs).equals(Code.xc_ids))
					{
						dnt.set(MonthTotal.column_xccjrs, dnt.getInt(MonthTotal.column_xccjrs)+1);//宣传成交人数
					}else
					{
						dnt.set(MonthTotal.column_zrjdcjrs, dnt.getInt(MonthTotal.column_zrjdcjrs)+1);//自然进店成交人数
					}
				}
				if (!dnt.update()) {
					log.debug("更新月综合查询失败！！");
					throw new RuntimeException("更新月综合查询失败!");
				}
			}else
			{
				dnt = new MonthTotal();
				dnt.set(MonthTotal.column_organids, t.getStr(Temporary.column_organIds))
					.set(MonthTotal.column_date, ToolDateTime.format(t.getDate(Temporary.column_gxsj)!=null ? t.getDate(Temporary.column_gxsj) : t.getDate(Temporary.column_createDate), ToolDateTime.pattern_ym))
					.set(MonthTotal.column_zxrs, 1);//咨询人数
				if(t.getStr(Temporary.column_gkfl).equals(Code.cjCus_ids))
				{
					dnt.set(MonthTotal.column_cjrs, 1);//成交人数
					double  cjl = ((double)dnt.getInt(MonthTotal.column_cjrs))/((double)dnt.getInt(MonthTotal.column_zxrs));
					dnt.set(MonthTotal.column_xgkCjl,new DecimalFormat("#.00").format(cjl*100));//成交率
					if(t.getStr(Temporary.column_jdfs).equals(Code.zjs_ids))
					{
						dnt.set(MonthTotal.column_zjscjrs, 1);//转介绍成交人数
					}
					else if(t.getStr(Temporary.column_jdfs).equals(Code.xc_ids))
					{
						dnt.set(MonthTotal.column_xccjrs, 1);//宣传成交人数
					}else
					{
						dnt.set(MonthTotal.column_zrjdcjrs, 1);//自然进店成交人数
					}
				}
				
				if (!dnt.save()) {
					log.debug("保存月综合查询失败！！");
					throw new RuntimeException("保存月综合查询失败!");
				}		
			}
		}	
		
		String cusSql = "SELECT  *  from  sb_customer and dataStatus in ('1','2')";
		List<Customer>  cList = Customer.dao.find(cusSql);
		for(Customer c : cList)
		{	
			MonthTotal dntLc = MonthTotal.dao.getMonthTotal(c.getStr(Customer.column_organIds),c.getDate(Customer.column_registtime));
			if(dntLc != null)
			{
				if(c.getCustomerAgreement() != null && null != c.getCustomerAgreement().getStr(CustomerAgreement.column_codeIds) && c.getCustomerAgreement().getStr(CustomerAgreement.column_codeIds).equals(Code.dlc_ids))
				{
					dntLc.set(MonthTotal.column_xgkDlcs, dntLc.getInt(MonthTotal.column_xgkDlcs)+1);//单疗程数
				}else
				{
					dntLc.set(MonthTotal.column_xgkLcs, dntLc.getInt(MonthTotal.column_xgkLcs)+(c.getCustomerAgreement() != null ? c.getCustomerAgreement().getInt(CustomerAgreement.column_treatment):0));//疗程数
				}
				dntLc.set(MonthTotal.column_xgkYjjs,c.getCustomerDetail().getFloat(CustomerDetail.column_delWeight));//应减斤数
				if (!dntLc.update()) {
					log.debug("更新月综合查询失败！！");
					throw new RuntimeException("更新月综合查询失败!");
				}
			}else
			{	
				dntLc = new MonthTotal();
				dntLc.set(MonthTotal.column_organids,c.getStr(Customer.column_organIds))
					 .set(MonthTotal.column_date, ToolDateTime.format(c.getDate(Customer.column_registtime), ToolDateTime.pattern_ym));
				if( c.getCustomerAgreement() != null && null != c.getCustomerAgreement().getStr(CustomerAgreement.column_codeIds) && c.getCustomerAgreement().getStr(CustomerAgreement.column_codeIds).equals(Code.dlc_ids))
				{
					dntLc.set(MonthTotal.column_xgkDlcs,1);//单疗程数
				}else
				{
					dntLc.set(MonthTotal.column_xgkLcs, c.getCustomerAgreement().getInt(CustomerAgreement.column_treatment));//疗程数
				}
				dntLc.set(MonthTotal.column_xgkYjjs,c.getCustomerDetail().getFloat(CustomerDetail.column_delWeight));//应减斤数
				if (!dntLc.save()) {
					log.debug("保存月综合查询失败！！");
					throw new RuntimeException("保存月综合查询失败!");
				}	
			}
			
			//保存到店情况
			//根据顾客查询所有到店服务情况
			String serSql = "SELECT *  FROM  sb_customer_service  WHERE  customerID = ? and dataStatus in ('1','2')";
			List<CustomerService>  csList = new CustomerService().dao.find(serSql,c.getStr(Customer.column_ids));
			if(csList != null && csList.size()>0)
			{	
				for(CustomerService  cs : csList)
				{
					MonthTotal dntFw = MonthTotal.dao.getMonthTotal(cs.getStr(CustomerService.column_organids), cs.getDate(CustomerService.column_startTime));
					if(dntFw != null)
					{
						
						dntFw.set(MonthTotal.column_fwcs, dntFw.getInt(MonthTotal.column_fwcs)+1);//服务次数
						if(null != cs.getFloat(CustomerService.column_weight) && cs.getFloat(CustomerService.column_weight) < Float.parseFloat(c.getCustomerDetail().getStr(CustomerDetail.column_standardWeight)) && null !=  cs.getFloat(CustomerService.column_lastWeight)  &&  cs.getFloat(CustomerService.column_lastWeight) > Float.parseFloat(c.getCustomerDetail().getStr(CustomerDetail.column_standardWeight)))
						{
							dntFw.set(MonthTotal.column_dbrs, dntFw.getInt(MonthTotal.column_dbrs)+1);//达标人数
						}
						if (!dntFw.update()) {
							log.debug("保存月综合查询失败！！");
							throw new RuntimeException("保存月综合查询失败!");
						}
					}else
					{
						dntFw = new MonthTotal();
						dntFw.set(MonthTotal.column_organids, cs.getStr(CustomerService.column_organids))
							.set(MonthTotal.column_date,  ToolDateTime.format(cs.getDate(CustomerService.column_startTime),ToolDateTime.pattern_ym))
							.set(MonthTotal.column_fwcs,1);//服务次数
						if(cs.getFloat(CustomerService.column_weight) < Float.parseFloat(c.getCustomerDetail().getStr(CustomerDetail.column_standardWeight)) && cs.getFloat(CustomerService.column_lastWeight) > Float.parseFloat(c.getCustomerDetail().getStr(CustomerDetail.column_standardWeight)))
						{
							dntFw.set(MonthTotal.column_dbrs,1);//达标人数
						}
						if (!dntFw.save()) {
							log.debug("保存月综合查询失败！！");
							throw new RuntimeException("保存月综合查询失败!");
						}	
					}
				}
			}
			String serddSql = "SELECT *  FROM  sb_customer_service  WHERE  customerID = ?  and dataStatus in ('1','2') GROUP BY  DATE_FORMAT(startTime,'%Y-%m')";
			List<CustomerService>  csddList = new CustomerService().dao.find(serddSql,c.getStr(Customer.column_ids));
			if(csddList != null && csddList.size()>0)
			{
				for(CustomerService  csdd : csddList)
				{
					MonthTotal dntDd = MonthTotal.dao.getMonthTotal(csdd.getStr(CustomerService.column_organids), csdd.getDate(CustomerService.column_startTime));
					if(dntDd != null)
					{
						dntDd.set(MonthTotal.column_sddrs, dntDd.getInt(MonthTotal.column_sddrs)+1)//实到店人数
							 .set(MonthTotal.column_fwrs, dntDd.getInt(MonthTotal.column_fwrs)+1);//服务人数
						if (!dntDd.update()) {
							log.debug("保存月综合查询失败！！");
							throw new RuntimeException("保存月综合查询失败!");
						}
					}else
					{
						dntDd = new MonthTotal();
						dntDd.set(MonthTotal.column_organids, csdd.getStr(CustomerService.column_organids))
							.set(MonthTotal.column_date,  ToolDateTime.format(csdd.getDate(CustomerService.column_startTime),ToolDateTime.pattern_ym))
							.set(MonthTotal.column_sddrs,1)//实到店人数
							 .set(MonthTotal.column_fwrs,1);//服务人数
						if (!dntDd.save()) {
							log.debug("保存月综合查询失败！！");
							throw new RuntimeException("保存月综合查询失败!");
						}
					}
				}
			}
			
			
			//保存掉秤斤数、涨称斤数、涨称人数、掉秤人数、平称人数、掉秤人数占比
			/*String sqlOrg = "SELECT organids from sb_customer_service_nurse GROUP BY organids";
			List<Organization> organizations  = Organization.dao.find(sqlOrg);
			for(Organization org:organizations){
				String cudid = org.getStr("organids");
				String  mdsql = "SELECT  COUNT(ids) dcrs,SUM(gjWeight) dcjs from  sb_customer_service_nurse  WHERE  organids = ?  AND  YearMon = ?  AND gjWeight >0";
				Record  cdsn =  Db.findFirst(mdsql,cudid,ToolDateTime.format(new Date(), ToolDateTime.pattern_ym));
				String  mzsql = "SELECT  COUNT(ids) zcrs,SUM(gjWeight) zcjs from  sb_customer_service_nurse  WHERE  organids = ?  AND  YearMon = ?  AND gjWeight <0";
				Record  czsn =  Db.findFirst(mzsql,cudid,ToolDateTime.format(new Date(), ToolDateTime.pattern_ym));
				String  mpsql = "SELECT  COUNT(ids) pcrs from  sb_customer_service_nurse  WHERE  organids = ?  AND  YearMon = ?  AND gjWeight =0";
				Record  cpsn =  Db.findFirst(mpsql,cudid,ToolDateTime.format(new Date(), ToolDateTime.pattern_ym));
				MonthTotal dntzdp =MonthTotal.dao.getMonthTotal(cudid, csdd.getDate(CustomerService.column_startTime));
				if(mt != null)
				{	
					
					String nowDate = ToolDateTime.format(new Date(), ToolDateTime.pattern_ym);
					String firstDate = ToolDateTime.format(cdet.getDate(CustomerDetail.column_firstDate), ToolDateTime.pattern_ym);
					if(nowDate.equals(firstDate)){
						
					}
					
//					if(null == csList || csList.size() ==0)
//					{
//						mt.set(MonthTotal.column_sddrs, mt.getInt(MonthTotal.column_sddrs)+1)
//						.set(MonthTotal.column_fwrs, mt.getInt(MonthTotal.column_fwrs)+1);
//					}
//						mt.set(MonthTotal.column_fwcs, mt.getInt(MonthTotal.column_fwcs)+1);
					if(cdsn != null && cdsn.getLong("dcrs") != 0)  
					{
						mt.set(MonthTotal.column_dcrs,Integer.parseInt(cdsn.getLong("dcrs")+""))
						.set(MonthTotal.column_dcjs, cdsn.getDouble("dcjs"));
						
					}
					if(czsn != null && czsn.getLong("zcrs") != 0)
					{
						mt.set(MonthTotal.column_zcrs,Integer.parseInt(czsn.getLong("zcrs")+""))
						.set(MonthTotal.column_zcjs, -czsn.getDouble("zcjs"));
					}
					if(cpsn != null && cpsn.getLong("pcrs") != 0)
					{
						mt.set(MonthTotal.column_pcrs,Integer.parseInt(cpsn.getLong("pcrs")+""));
					}
					double  mdcbl = ((double)mt.getInt(MonthTotal.column_dcrs))/((double)mt.getInt(MonthTotal.column_sddrs));
					mt.set(MonthTotal.column_dcrszb,new DecimalFormat("#.00").format(mdcbl*100));
					if (!mt.update()) {
						log.debug("更新月综合查询表失败");
						throw new RuntimeException("更新月综合查询表失败!");
					}
				}
			}
*/
			
			
			//保存销售情况
			
			String  saleSql = "SELECT spsd.*,sp.proBh,sp.typeIds FROM  sb_product_sales_detail  spsd LEFT JOIN  sb_product sp ON  spsd.productID = sp.ids "+
					      " WHERE  customerID = ? and dataStatus in (1,2)";
			List<ProductSales>  psList = ProductSales.dao.find(saleSql,c.getStr(Customer.column_ids));
			if(psList != null )
			{
				for(ProductSales ps : psList)
				{
					MonthTotal dntXs = MonthTotal.dao.getMonthTotal(ps.getStr(ProductSales.column_organids), ps.getDate(ProductSales.column_date));
					if(dntXs != null)
					{
						dntXs.set(ps.getStr(Product.column_proBh)+"SalNum",dntXs.getInt(ps.getStr(Product.column_proBh)+"SalNum")+ps.getInt(ProductSales.column_number))//产品销售数量
							 .set(ps.getStr(Product.column_proBh)+"SalPrice", dntXs.getDouble(ps.getStr(Product.column_proBh)+"SalPrice")+ps.getFloat(ProductSales.column_price))//产品销售金额
							 .set(MonthTotal.column_priceTotal, dntXs.getDouble(MonthTotal.column_priceTotal)+ps.getFloat(ProductSales.column_price));//销售总金额
						if(ps.getStr(Product.column_typeIds).equals(Code.dlc_ids) || ps.getStr(Product.column_typeIds).equals(Code.lc_ids))
						{
							dntXs.set(MonthTotal.column_jzzxse,dntXs.getDouble(MonthTotal.column_jzzxse)+ps.getFloat(ProductSales.column_price));//减重销售总金额
						}
						if (!dntXs.update()) {
							log.debug("保存月综合查询失败！！");
							throw new RuntimeException("保存月综合查询失败!");
						}
					}else
					{
						dntXs = new MonthTotal();
						dntXs.set(MonthTotal.column_organids, ps.getStr(ProductSales.column_organids))
							 .set(MonthTotal.column_date,  ToolDateTime.format(ps.getDate(ProductSales.column_date),ToolDateTime.pattern_ym))
							 .set(ps.getStr(Product.column_proBh)+"SalNum",ps.getInt(ProductSales.column_number))//产品销售数量
							 .set(ps.getStr(Product.column_proBh)+"SalPrice", ps.getFloat(ProductSales.column_price))//产品销售金额
							 .set(MonthTotal.column_priceTotal,ps.getFloat(ProductSales.column_price));//销售总金额
						if(ps.getStr(Product.column_typeIds).equals(Code.dlc_ids) || ps.getStr(Product.column_typeIds).equals(Code.lc_ids))
						{
							dntXs.set(MonthTotal.column_jzzxse,ps.getFloat(ProductSales.column_price));//减重销售总金额
						}
						if (!dntXs.save()) {
							log.debug("保存月综合查询失败！！");
							throw new RuntimeException("保存月综合查询失败!");
						}
					}
				}
			}
		}
		
	}
	
	
	/**保存旧数据到年综合查询
	 * 
	 */
	public  void  saveYearOld()
	{
		String  sql  = "SELECT *  FROM  sb_customer_temporary  and dataStatus in ('1','2')";
		List<Temporary>  temporaryList = Temporary.dao.find(sql);
		for(Temporary t:temporaryList)
		{
			//通过测算机构和日期查询日综合查询表数据
			//保存更新顾客统计信息
			YearTotal dnt = YearTotal.dao.getYearTotal(t.getStr(Temporary.column_organIds), t.getDate(Temporary.column_gxsj)!=null ? t.getDate(Temporary.column_gxsj) : t.getDate(Temporary.column_createDate));
			if(dnt!=null)
			{
				dnt.set(YearTotal.column_zxrs, dnt.getInt(YearTotal.column_zxrs)+1);//咨询人数
				if(t.getStr(Temporary.column_gkfl).equals(Code.cjCus_ids))
				{
					dnt.set(YearTotal.column_cjrs, dnt.getInt(YearTotal.column_cjrs)+1);//成交人数
					double  cjl = ((double)dnt.getInt(YearTotal.column_cjrs))/((double)dnt.getInt(YearTotal.column_zxrs));
					dnt.set(YearTotal.column_xgkCjl,new DecimalFormat("#.00").format(cjl*100));//成交率
					if(t.getStr(Temporary.column_jdfs).equals(Code.zjs_ids))
					{
						dnt.set(YearTotal.column_zjscjrs, dnt.getInt(YearTotal.column_zjscjrs)+1);//转介绍成交人数
					}
					else if(t.getStr(Temporary.column_jdfs).equals(Code.xc_ids))
					{
						dnt.set(YearTotal.column_xccjrs, dnt.getInt(YearTotal.column_xccjrs)+1);//宣传成交人数
					}else
					{
						dnt.set(YearTotal.column_zrjdcjrs, dnt.getInt(YearTotal.column_zrjdcjrs)+1);//自然进店成交人数
					}
				}
				if (!dnt.update()) {
					log.debug("更新年综合查询失败！！");
					throw new RuntimeException("更新年综合查询失败!");
				}
			}else
			{
				dnt = new YearTotal();
				dnt.set(YearTotal.column_organids, t.getStr(Temporary.column_organIds))
					.set(YearTotal.column_date, ToolDateTime.format(t.getDate(Temporary.column_gxsj)!=null ? t.getDate(Temporary.column_gxsj) : t.getDate(Temporary.column_createDate), ToolDateTime.pattern_y))
					.set(YearTotal.column_zxrs, 1);//咨询人数
				if(t.getStr(Temporary.column_gkfl).equals(Code.cjCus_ids))
				{
					dnt.set(YearTotal.column_cjrs, 1);//成交人数
					double  cjl = ((double)dnt.getInt(YearTotal.column_cjrs))/((double)dnt.getInt(YearTotal.column_zxrs));
					dnt.set(YearTotal.column_xgkCjl,new DecimalFormat("#.00").format(cjl*100));//成交率
					if(t.getStr(Temporary.column_jdfs).equals(Code.zjs_ids))
					{
						dnt.set(YearTotal.column_zjscjrs, 1);//转介绍成交人数
					}
					else if(t.getStr(Temporary.column_jdfs).equals(Code.xc_ids))
					{
						dnt.set(YearTotal.column_xccjrs, 1);//宣传成交人数
					}else
					{
						dnt.set(YearTotal.column_zrjdcjrs, 1);//自然进店成交人数
					}
				}
				
				if (!dnt.save()) {
					log.debug("保存年综合查询失败！！");
					throw new RuntimeException("保存年综合查询失败!");
				}		
			}
		}	
		
		String cusSql = "SELECT  *  from  sb_customer and dataStatus in ('1','2')";
		List<Customer>  cList = Customer.dao.find(cusSql);
		for(Customer c : cList)
		{	
			YearTotal dntLc = YearTotal.dao.getYearTotal(c.getStr(Customer.column_organIds),c.getDate(Customer.column_registtime));
			if(dntLc != null)
			{
				if(c.getCustomerAgreement() != null && null != c.getCustomerAgreement().getStr(CustomerAgreement.column_codeIds) && c.getCustomerAgreement().getStr(CustomerAgreement.column_codeIds).equals(Code.dlc_ids))
				{
					dntLc.set(YearTotal.column_xgkDlcs, dntLc.getInt(YearTotal.column_xgkDlcs)+1);//单疗程数
				}else
				{
					dntLc.set(YearTotal.column_xgkLcs, dntLc.getInt(YearTotal.column_xgkLcs)+(c.getCustomerAgreement() != null ? c.getCustomerAgreement().getInt(CustomerAgreement.column_treatment):0));//疗程数
				}
				dntLc.set(YearTotal.column_xgkYjjs,c.getCustomerDetail().getFloat(CustomerDetail.column_delWeight));//应减斤数
				if (!dntLc.update()) {
					log.debug("更新年综合查询失败！！");
					throw new RuntimeException("更新年综合查询失败!");
				}
			}else
			{	
				dntLc = new YearTotal();
				dntLc.set(YearTotal.column_organids,c.getStr(Customer.column_organIds))
					 .set(YearTotal.column_date, ToolDateTime.format(c.getDate(Customer.column_registtime), ToolDateTime.pattern_y));
				if( c.getCustomerAgreement() != null && null != c.getCustomerAgreement().getStr(CustomerAgreement.column_codeIds) && c.getCustomerAgreement().getStr(CustomerAgreement.column_codeIds).equals(Code.dlc_ids))
				{
					dntLc.set(YearTotal.column_xgkDlcs,1);//单疗程数
				}else
				{
					dntLc.set(YearTotal.column_xgkLcs,c.getCustomerAgreement() != null ? c.getCustomerAgreement().getInt(CustomerAgreement.column_treatment):0);//疗程数
				}
				dntLc.set(YearTotal.column_xgkYjjs,c.getCustomerDetail().getFloat(CustomerDetail.column_delWeight));//应减斤数
				if (!dntLc.save()) {
					log.debug("保存年综合查询失败！！");
					throw new RuntimeException("保存年综合查询失败!");
				}	
			}
			
			//保存到店情况
			//根据顾客查询所有到店服务情况
			String serSql = "SELECT *  FROM  sb_customer_service  WHERE  customerID = ? and dataStatus in ('1','2')";
			List<CustomerService>  csList = new CustomerService().dao.find(serSql,c.getStr(Customer.column_ids));
			if(csList != null && csList.size()>0)
			{	
				for(CustomerService  cs : csList)
				{
					YearTotal dntFw = YearTotal.dao.getYearTotal(cs.getStr(CustomerService.column_organids), cs.getDate(CustomerService.column_startTime));
					if(dntFw != null)
					{
						
						dntFw.set(YearTotal.column_fwcs, dntFw.getInt(YearTotal.column_fwcs)+1);//服务次数
						if(null != cs.getFloat(CustomerService.column_weight) && cs.getFloat(CustomerService.column_weight) < Float.parseFloat(c.getCustomerDetail().getStr(CustomerDetail.column_standardWeight)) && null != cs.getFloat(CustomerService.column_lastWeight) && cs.getFloat(CustomerService.column_lastWeight) > Float.parseFloat(c.getCustomerDetail().getStr(CustomerDetail.column_standardWeight)))
						{
							dntFw.set(YearTotal.column_dbrs, dntFw.getInt(YearTotal.column_dbrs)+1);//达标人数
						}
						if (!dntFw.update()) {
							log.debug("保存年综合查询失败！！");
							throw new RuntimeException("保存年综合查询失败!");
						}
					}else
					{
						dntFw = new YearTotal();
						dntFw.set(YearTotal.column_organids, cs.getStr(CustomerService.column_organids))
							.set(YearTotal.column_date,  ToolDateTime.format(cs.getDate(CustomerService.column_startTime),ToolDateTime.pattern_y))
							.set(YearTotal.column_fwcs,1);//服务次数
						if(cs.getFloat(CustomerService.column_weight) < Float.parseFloat(c.getCustomerDetail().getStr(CustomerDetail.column_standardWeight)) && cs.getFloat(CustomerService.column_lastWeight) > Float.parseFloat(c.getCustomerDetail().getStr(CustomerDetail.column_standardWeight)))
						{
							dntFw.set(YearTotal.column_dbrs,1);//达标人数
						}
						if (!dntFw.save()) {
							log.debug("保存年综合查询失败！！");
							throw new RuntimeException("保存年综合查询失败!");
						}	
					}
				}
			}
			String serddSql = "SELECT *  FROM  sb_customer_service  WHERE  customerID = ?  and dataStatus in ('1','2') GROUP BY  DATE_FORMAT(startTime,'%Y')";
			List<CustomerService>  csddList = new CustomerService().dao.find(serddSql,c.getStr(Customer.column_ids));
			if(csddList != null && csddList.size()>0)
			{
				for(CustomerService  csdd : csddList)
				{
					YearTotal dntDd = YearTotal.dao.getYearTotal(csdd.getStr(CustomerService.column_organids), csdd.getDate(CustomerService.column_startTime));
					if(dntDd != null)
					{
						dntDd.set(YearTotal.column_sddrs, dntDd.getInt(YearTotal.column_sddrs)+1)//实到店人数
							 .set(YearTotal.column_fwrs, dntDd.getInt(YearTotal.column_fwrs)+1);//服务人数
						if (!dntDd.update()) {
							log.debug("保存年综合查询失败！！");
							throw new RuntimeException("保存年综合查询失败!");
						}
					}else
					{
						dntDd = new YearTotal();
						dntDd.set(YearTotal.column_organids, csdd.getStr(CustomerService.column_organids))
							.set(YearTotal.column_date,  ToolDateTime.format(csdd.getDate(CustomerService.column_startTime),ToolDateTime.pattern_y))
							.set(YearTotal.column_sddrs,1)//实到店人数
							 .set(YearTotal.column_fwrs,1);//服务人数
						if (!dntDd.save()) {
							log.debug("保存年综合查询失败！！");
							throw new RuntimeException("保存年综合查询失败!");
						}
					}
				}
			}
			
			//保存销售情况
			
			String  saleSql = "SELECT spsd.*,sp.proBh,sp.typeIds FROM  sb_product_sales_detail  spsd LEFT JOIN  sb_product sp ON  spsd.productID = sp.ids "+
					      " WHERE  customerID = ? and dataStatus in (1,2)";
			List<ProductSales>  psList = ProductSales.dao.find(saleSql,c.getStr(Customer.column_ids));
			if(psList != null )
			{
				for(ProductSales ps : psList)
				{
					YearTotal dntXs = YearTotal.dao.getYearTotal(ps.getStr(ProductSales.column_organids), ps.getDate(ProductSales.column_date));
					if(dntXs != null)
					{
						dntXs.set(ps.getStr(Product.column_proBh)+"SalNum",dntXs.getInt(ps.getStr(Product.column_proBh)+"SalNum")+ps.getInt(ProductSales.column_number))//产品销售数量
							 .set(ps.getStr(Product.column_proBh)+"SalPrice", dntXs.getDouble(ps.getStr(Product.column_proBh)+"SalPrice")+ps.getFloat(ProductSales.column_price))//产品销售金额
							 .set(YearTotal.column_priceTotal, dntXs.getDouble(YearTotal.column_priceTotal)+ps.getFloat(ProductSales.column_price));//销售总金额
						if(ps.getStr(Product.column_typeIds).equals(Code.dlc_ids) || ps.getStr(Product.column_typeIds).equals(Code.lc_ids))
						{
							dntXs.set(YearTotal.column_jzzxse,dntXs.getDouble(YearTotal.column_jzzxse)+ps.getFloat(ProductSales.column_price));//减重销售总金额
						}
						if (!dntXs.update()) {
							log.debug("保存年综合查询失败！！");
							throw new RuntimeException("保存年综合查询失败!");
						}
					}else
					{
						dntXs = new YearTotal();
						dntXs.set(YearTotal.column_organids, ps.getStr(ProductSales.column_organids))
							 .set(YearTotal.column_date,  ToolDateTime.format(ps.getDate(ProductSales.column_date),ToolDateTime.pattern_y))
							 .set(ps.getStr(Product.column_proBh)+"SalNum",ps.getInt(ProductSales.column_number))//产品销售数量
							 .set(ps.getStr(Product.column_proBh)+"SalPrice", ps.getFloat(ProductSales.column_price))//产品销售金额
							 .set(YearTotal.column_priceTotal,ps.getFloat(ProductSales.column_price));//销售总金额
						if(ps.getStr(Product.column_typeIds).equals(Code.dlc_ids) || ps.getStr(Product.column_typeIds).equals(Code.lc_ids))
						{
							dntXs.set(YearTotal.column_jzzxse,ps.getFloat(ProductSales.column_price));//减重销售总金额
						}
						if (!dntXs.save()) {
							log.debug("保存年综合查询失败！！");
							throw new RuntimeException("保存年综合查询失败!");
						}
					}
				}
			}
		}
	}
	
	
	
	
	/**
	*添加掉秤人数，涨称人数，平称人数，掉秤比例，使用产品人数，使用产品占比
	*到日，月，年，综合查询
	 * @throws ParseException 
	* 
	*/
	public void  saveRynTotal() throws ParseException
	{	
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM");
		String cusSql = "SELECT  *  from  sb_customer and dataStatus in ('1','2')";
		List<Customer>  cList = Customer.dao.find(cusSql);
		for(Customer c : cList)
		{	
			String serSql = "SELECT *  FROM  sb_customer_service  WHERE  customerID = ? and dataStatus in ('1','2')";
			List<CustomerService>  csList = new CustomerService().dao.find(serSql,c.getStr(Customer.column_ids));
			if(csList != null && csList.size()>0)
			{	
				for(CustomerService  cs : csList)
				{	
					//日
					DailyNursingTotal  dnt =DailyNursingTotal.dao.getDnt(cs.getStr(CustomerService.column_organids), cs.getDate(CustomerService.column_startTime));
					if(dnt != null)
					{	
						
						double  dcbl = 0;
						double  cpbl = 0;
//						dnt.set(DailyNursingTotal.column_sycprszb,new DecimalFormat("#.00").format(cpbl*100));
						if(null != c.getCustomerDetail().getStr(CustomerDetail.column_customerType) && c.getCustomerDetail().getStr(CustomerDetail.column_customerType).equals("0"))
						{	
							float js = cs.getFloat(CustomerService.column_weight) - cs.getFloat(CustomerService.column_lastWeight);
							if(js < 0)
							{
								dnt.set(DailyNursingTotal.column_dcrs,dnt.getInt(DailyNursingTotal.column_dcrs)+1)///掉秤人数
									.set(DailyNursingTotal.column_dcjs, dnt.getDouble(DailyNursingTotal.column_dcjs)+(-js));//掉秤斤数
//								dcbl = ((double)dnt.getInt(DailyNursingTotal.column_dcrs))/((double)dnt.getInt(DailyNursingTotal.column_sddrs));
							}
							if(js >0)
							{
								dnt.set(DailyNursingTotal.column_zcrs, dnt.getInt(DailyNursingTotal.column_zcrs)+1)//涨称人数
									.set(DailyNursingTotal.column_zcjs, dnt.getDouble(DailyNursingTotal.column_zcjs)+js);//涨称斤数
							}
							if(js==0)
							{
								dnt.set(DailyNursingTotal.column_pcrs,dnt.getInt(DailyNursingTotal.column_pcrs)+1);//平称人数
							}
						}
						dcbl = ((double)dnt.getInt(DailyNursingTotal.column_dcrs))/((double)dnt.getInt(DailyNursingTotal.column_sddrs));
						dnt.set(DailyNursingTotal.column_dcrszb,new DecimalFormat("#.00").format(dcbl*100));//掉秤人数占比
						if(cs.getStr(CustomerService.column_isShaping) != null && cs.getStr(CustomerService.column_isShaping).equals("1"))
						{
							dnt.set(DailyNursingTotal.column_sycprs, dnt.getInt(DailyNursingTotal.column_sycprs)+1);
							cpbl = ((double)dnt.getInt(DailyNursingTotal.column_sycprs))/((double)dnt.getInt(DailyNursingTotal.column_sddrs));
						}
						dnt.set(DailyNursingTotal.column_sycprszb,new DecimalFormat("#.00").format(cpbl*100));//使用产品人数占比
						if (!dnt.update()) {
							log.debug("保存日综合查询失败！！");
							throw new RuntimeException("保存日综合查询失败!");
						}
					}
					
				}
			}
			String nsql = "SELECT  *  FROM  sb_customer_service_nurse  WHERE organids = ? AND customerID = ?  and dataStatus in ('1','2') ";
			List<CustomerServiceNurse>  csnList =  CustomerServiceNurse.dao.find(nsql,c.getStr(Customer.column_organIds),c.getStr(Customer.column_ids));
			if(null != csnList && csnList.size()>0)
			{
				for(CustomerServiceNurse csn : csnList)
				{
					MonthTotal mt = MonthTotal.dao.getMonthTotal(csn.getStr(CustomerServiceNurse.column_organids), sdf.parse(csn.getStr(CustomerServiceNurse.column_YearMon)));
					if(mt != null)
					{	
						double  dcbl = 0;
						if(csn.getFloat(CustomerServiceNurse.column_gjWeight) < 0)
						{
							mt.set(MonthTotal.column_dcrs, mt.getInt(MonthTotal.column_dcrs)+1)
							.set(MonthTotal.column_dcjs, mt.getDouble(MonthTotal.column_dcjs)+(-csn.getFloat(CustomerServiceNurse.column_gjWeight)));
							dcbl = ((double)mt.getInt(DailyNursingTotal.column_dcrs))/((double)mt .getInt(DailyNursingTotal.column_sddrs));
						}
						if(csn.getFloat(CustomerServiceNurse.column_gjWeight) > 0)
						{
							mt.set(MonthTotal.column_zcrs, mt.getInt(MonthTotal.column_zcrs)+1)
							.set(MonthTotal.column_zcjs, mt.getDouble(MonthTotal.column_zcjs)+csn.getFloat(CustomerServiceNurse.column_gjWeight));
						}
						if(csn.getFloat(CustomerServiceNurse.column_gjWeight) == 0)
						{
							mt.set(MonthTotal.column_pcrs, mt.getInt(MonthTotal.column_pcrs)+1);
						}
						mt.set(MonthTotal.column_dcrszb, new DecimalFormat("#.00").format(dcbl*100));
						if (!mt.update()) {
							log.debug("保存月综合查询失败！！");
							throw new RuntimeException("保存月综合查询失败!");
						}
					} 
				}
			}
			String scssql = "	SELECT  *  FROM  sb_customer_service  WHERE  customerID = ? and dataStatus in ('1','2') AND organids = ? AND isShaping = '1'"+
					"GROUP BY  DATE_FORMAT(startTime,'%Y-%m-%d')";
			List<CustomerService>    csyList = CustomerService.dao.find(scssql,c.getStr(Customer.column_ids),c.getStr(Customer.column_organIds));
			if(null != csyList && csyList.size()>0)
			{
				for(CustomerService cs :csyList)
				{	
					double  cpbl = 0;
					MonthTotal  mt1 = MonthTotal.dao.getMonthTotal(cs.getStr(CustomerService.column_organids), cs.getDate(CustomerService.column_startTime));
					if(mt1 != null)
					{
						mt1.set(MonthTotal.column_sycprs, mt1.getInt(MonthTotal.column_sycprs)+1);
						cpbl = ((double)mt1.getInt(DailyNursingTotal.column_sycprs))/((double)mt1.getInt(DailyNursingTotal.column_sddrs));
						mt1.set(MonthTotal.column_sycprszb, new DecimalFormat("#.00").format(cpbl*100));
						if (!mt1.update()) {
							log.debug("保存月综合查询失败！！");
							throw new RuntimeException("保存月综合查询失败!");
						}
					}
				}
			}
		}
		String  ytsql = "SELECT  *  FROM  sb_customer_total_year and dataStatus in ('1','2') "; 
		List<YearTotal>  ytList = YearTotal.dao.find(ytsql);
		if(null != ytList && ytList.size()>0)
		{
			for(YearTotal  yt : ytList)
			{
				Calendar calendar=Calendar.getInstance();
				int month=calendar.get(Calendar.MONTH)+1;
				int year = calendar.get(Calendar.YEAR);
				int ssyear= Integer.parseInt(yt.getStr(YearTotal.column_date));
				if(ssyear != year)
				{
					month = 12;
				}
				int yjlcs = 0;
				if(yt.getInt(YearTotal.column_cjrs) != 0)
				{
					yjlcs = (yt.getInt(YearTotal.column_xgkLcs)/yt.getInt(YearTotal.column_cjrs))/month;
				}
//				yt.set(YearTotal.column_yjCjrs, yt.getInt(YearTotal.column_cjrs)/month)
					yt.set(YearTotal.column_yjLcs, yjlcs);
//					.set(YearTotal.column_yjfwrs, yt.getInt(YearTotal.column_fwrs)/month)
//					.set(YearTotal.column_yjfwcs, yt.getInt(YearTotal.column_fwcs)/month)
//					.set(YearTotal.column_yjdbrs, yt.getInt(YearTotal.column_dbrs)/month)
//					.set(YearTotal.column_yjjzxs, yt.getDouble(YearTotal.column_jzzxse)/month)
//					.set(YearTotal.column_yjzxs, yt.getDouble(YearTotal.column_priceTotal));
				if (!yt.update()) {
					log.debug("保存年综合查询失败！！");
					throw new RuntimeException("保存年综合查询失败!");
				}
				
			}
		}
	}
}
